package com.athena.view 
{
	import com.adobe.serialization.json.JSON;
	import com.athena.ui.cardgame;
	import flash.display.MovieClip;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author ...
	 */
	public class Card extends cardgame
	{
		private var _number : int;
		private var _ready : Boolean = false;
		private var _boundaries:Rectangle;
		private var startX : Number;
		private var startY : Number;
		private var hSelect : Number = 15;
		private var choose : Boolean = false;
		
		public static var PICK_CARD : String = "PICK_CARD";
		public static var CHECKING : String = "CHECKING_CARD";
		public static var DROP_CARD : String = "DROP_CARD";
		public function Card(num : int = 0) 
		{
			number = num;
			target.visible = false;
			gotoAndStop(num + 1);
			_boundaries = new Rectangle(200, 100, 650, 400);
            addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
		}
		
		private function _mouseDown(e:MouseEvent):void
        {
			if (_number == 0) return;
			if (!_ready) return;
			startX = x;
			startY = y;
			dispatchEvent(new Event(PICK_CARD));
			addEventListener(Event.ENTER_FRAME, checking);
            stage.addEventListener(MouseEvent.MOUSE_UP, _stopDrag);
        }
		
		private function checking(e : Event) : void {
			dispatchEvent(new DataEvent(CHECKING, true, false, JSON.encode(new Array(x, y, startX, startY))));
		}
		
		/**
         * MOUSE_UP
         */
        private function _stopDrag(e:MouseEvent) : void
        {
			e.target.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, _stopDrag);
			removeEventListener(Event.ENTER_FRAME, checking);
			dispatchEvent(new DataEvent(DROP_CARD, true, false, JSON.encode(new Array(x, y, startX, startY))));
        }
		
		public function setChoose() : void {
			choose = !choose;
			if (choose) y = y - hSelect;
			else y = y + hSelect;
		}
		public function removeEvent() : void {
			removeEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
		}
		public function setTarget(bool : Boolean) : void {
			target.visible = bool;
		}
		public function get number():int 
		{
			return _number;
		}
		
		public function set number(value:int):void 
		{
			_number = value;
		}
		
		public function get ready():Boolean 
		{
			return _ready;
		}
		
		public function set ready(value:Boolean):void 
		{
			_ready = value;
		}
		
	}

}